/*
 * Sistema Gestor de Proyectos, Version Alpha 1.01.22, Sistema Modular para la Gestión de Proyectos
 * Copyright (C) 2011 Marco Antonio Ramírez Sánchez
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package Clases_auxiliares;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class encriptacion_md5 {

    /**
     * Funcion que encripta a md5
     * @param p String el cual va a ser encriptado a md5
     * @return String con la palabra ya encriptada en md5
     */
    public String convertir_md5(String p) {
        try {
            String pass, pas = null;
            pass = p;
            byte[] defaultBytes = pass.getBytes();
            MessageDigest algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(defaultBytes);
            byte messageDigest[] = algorithm.digest();
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < messageDigest.length; i++) {
                String hex = Integer.toHexString(0xFF & messageDigest[i]);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            pas = hexString.toString();
            pass = hexString + "";
            return pas;
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(encriptacion_md5.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
